<script setup>
import { useMouseInElement } from '@vueuse/core'
// 图片列表
import { ref, watch } from 'vue'

const props = defineProps({
  imageList: {
    type: Array,
    default: () => [],
  },
})

const activeIndex = ref(0)
function enterHandler(e) {
  // 获取鼠标进入的图片
  // 获取当前图片的索引
  console.log('target', e)
  activeIndex.value = e
}
const target = ref(null)
const left = ref(0)
const top = ref(0)
const { elementX, elementY, isOutside } = useMouseInElement(target)
watch ([elementX, elementY], ([newx, newy], [oldx, oldy]) => {
  if (isOutside.value) return
  console.log('鼠标位置', newx, newy)
  if (elementX.value > 100 && elementX.value < 300) {
    left.value = elementX.value - 100
  }
  if (elementY.value > 100 && elementY.value < 300) {
    top.value = elementY.value - 100
  }
  // eslint-disable-next-line style/max-statements-per-line
  if (elementX.value > 300) { left.value = 200 }
  // eslint-disable-next-line style/max-statements-per-line
  if (elementY.value > 300) { top.value = 200 }
  // eslint-disable-next-line style/max-statements-per-line
  if (elementX.value < 100) { left.value = 0 }
  // eslint-disable-next-line style/max-statements-per-line
  if (elementY.value < 100) { top.value = 0 }
})
const positionX = ref(0)
const positionY = ref(0)
watch([left, top], ([newLeft, newTop]) => {
  // 计算放大镜背景图的偏移量
  positionX.value = -newLeft * 2
  positionY.value = -newTop * 2
})
</script>

<template>
  <div class="goods-image">
    <!-- 左侧大图 -->
    <div ref="target" class="middle">
      <img :src="imageList[activeIndex]" alt="" />
      <!-- 蒙层小滑块 -->
      <div v-show="!isOutside" class="layer" :style="{ left: `${left}px`, top: `${top}px` }"></div>
    </div>
    <!-- 小图列表 -->
    <ul class="small">
      <li v-for="(img, i) in props.imageList" :key="i" :class="{ active: activeIndex === i }" @mouseenter="enterHandler(i)">
        <img :src="img" alt="" />
      </li>
    </ul>
    <!-- 放大镜大图 -->
    <div
v-show="!isOutside" class="large" :style="[
      {
        backgroundImage: `url(${imageList[activeIndex]})`,
        backgroundPositionX: `${positionX}px`,
        backgroundPositionY: `${positionY}px`,
      },
    ]"
></div>
  </div>
</template>

<style scoped lang="scss">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;

  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
  }

  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index: 500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    // 背景图:盒子的大小 = 2:1  将来控制背景图的移动来实现放大的效果查看 background-position
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }

  .layer {
    width: 200px;
    height: 200px;
    background: rgba(0, 0, 0, 0.2);
    // 绝对定位 然后跟随咱们鼠标控制left和top属性就可以让滑块移动起来
    left: 0;
    top: 0;
    position: absolute;
  }

  .small {
    width: 80px;

    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;

      &:hover,
      &.active {
        border: 2px solid $xtxColor;
      }
    }
  }
}
</style>
